package com.leetcode.partition6;

/**
 * @author `RKC`
 * @date 2021/11/13 9:22
 */
public class LC520检测大写字母 {

    public boolean detectCapitalUse(String word) {
        if (word.length() == 1) return true;
        if (upper(word.charAt(0)) && upper(word.charAt(1))) {
            //应该全部是大写
            for (int i = 2; i < word.length(); i++) {
                if (!upper(word.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
        if (upper(word.charAt(0)) && !upper(word.charAt(1))) {
            //只应该是首字母大写
            for (int i = 1; i < word.length(); i++) {
                if (upper(word.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
        //全部小写
        for (int i = 1; i < word.length(); i++) {
            if (upper(word.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    private boolean upper(char c) {
        return c >= 'A' & c <= 'Z';
    }
}
